package com.kang.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.InventoryLocation;
import com.kang.service.InventoryLocationService;
import com.kang.service.WarehouseService;
import com.vonechina.entity.page.InventoryLocationPage;

/**
 * 库位控制层
 * 
 * @author zs614
 *
 */
@Controller
@RequestMapping("inventorylocation")
@SessionAttributes({ "inventoryLocationPage" })
public class InventorylocationController extends com.kang.controller.Controller {
	/**
	 * 库位服务层
	 */
	@Resource
	private InventoryLocationService locationService;
	/**
	 * 仓库服务层
	 */
	@Resource
	private WarehouseService warehouseService;

	/**
	 * 分页查询库位列表
	 * 
	 * @param session
	 * @param model
	 * @param inventoryLocationPage
	 *            搜索条件自动封装
	 * @return 返回jsp界面
	 */
	@RequestMapping("listInventoryLocation")
	public String listInventoryLocation(HttpSession session, Model model, InventoryLocationPage inventoryLocationPage) {
		initRight(session);
		if (checkRight("查看库位") != SUCCESS) {
			return "basic/noRight";
		}
		model.addAttribute("isAdd", checkRight("新增库位") == SUCCESS);
		model.addAttribute("isEdit", checkRight("编辑库位") == SUCCESS);
		model.addAttribute("isDelete", checkRight("删除库位") == SUCCESS);
		InventoryLocation inventorylocation = locationService
				.selectByPrimaryKey(inventoryLocationPage.getInventorylocationid());
		if (inventorylocation != null) {
			inventoryLocationPage.setFirstid(inventorylocation.getFirstid());
			inventoryLocationPage.setSecondid(inventorylocation.getSecondid() + 1);
		} else {
			inventoryLocationPage.setFirstid(0);
			inventoryLocationPage.setSecondid(1);
		}
		model.addAttribute("warehoseList", warehouseService.listAllWarehouse());
		model.addAttribute("inventoryLocationList", locationService.listInventoryLocation(inventoryLocationPage));
		inventoryLocationPage.setPageUrl("inventorylocation/listInventoryLocation.do");
		model.addAttribute("page", inventoryLocationPage);
		return "basicData/inventoryLocation/listInventoryLocation";
	}

	/**
	 * 通过库位ID查询库位信息
	 * 
	 * @param session
	 * @param inventorylocationid
	 *            库位ID
	 * @return 返回json字符串
	 */
	@RequestMapping("findInventoryLocationById")
	@ResponseBody
	public String findInventoryLocationById(HttpSession session, Integer inventorylocationid) {
		InventoryLocation inventorylocation = locationService.selectByPrimaryKey(inventorylocationid);
		return JSON.toJSONString(inventorylocation);
	}

	/**
	 *  编辑库位信息
	 * @param session
	 * @param inventorylocation 库位实体类自动封装
	 * @return 返回json字符串
	 */
	@RequestMapping("update")
	@ResponseBody
	public String updateinventorylocation(HttpSession session, InventoryLocation inventorylocation) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("编辑库位") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int a = locationService.updateByPrimaryKeySelective(inventorylocation);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 *  根据库位删除库位
	 * @param session
	 * @param inventorylocationid 库位ID
	 * @return 返回json字符串
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String deleteinventorylocation(HttpSession session, Integer inventorylocationid) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("删除库位") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		InventoryLocation inventorylocation = locationService.selectByPrimaryKey(inventorylocationid);
		if (inventorylocation.getSecondid() == 1) {
			List<InventoryLocation> list = locationService.listInventoryLocationById(inventorylocationid);
			for (InventoryLocation cate : list) {
				locationService.deletenext(cate.getInventorylocationid());
			}
			locationService.deletenext(inventorylocationid);
		} else if (inventorylocation.getSecondid() == 2) {
			locationService.deletenext(inventorylocationid);
		}
		int a = locationService.deleteByPrimaryKey(inventorylocationid);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 *  新增库位
	 * @param session
	 * @param inventorylocation 库位实体类自动封装
	 * @return 返回json字符串
	 */
	@RequestMapping("add")
	@ResponseBody
	public String addinventorylocation(HttpSession session, InventoryLocation inventorylocation) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("新增库位") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		inventorylocation.setInventorylocationcode(locationService.generatoCode());
		InventoryLocation cg = locationService.selectByPrimaryKey(inventorylocation.getFirstid());
		if (cg == null) {
			inventorylocation.setFirstid(0);
			inventorylocation.setSecondid(1);
		} else {
			inventorylocation.setSecondid(cg.getSecondid() + 1);
			inventorylocation.setWarehouseManagementID(cg.getWarehouseManagementID());
		}
		inventorylocation.setCreatetime(new Date());
		inventorylocation.setCreatepersonid((Integer) session.getAttribute("userID"));
		int a = locationService.insertSelective(inventorylocation);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

}
